<#--<timed-task-upsert v-if="dialogProps.showCreateJobDialog"-->
<#--:id="activeId"-->
<#--@close="handleCloseCreateJobDialog"-->
<#--></timed-task-upsert>-->
<script type="text/x-template" id="timed-task-upsert">
    <custom-dialog class="batch-file-upload-dialog" :options="dialogOptions"
                   :closefn="close">
        <div slot="dialogContent">
            <div class="action-list">
                <div style="padding: 10px;">
                    <el-button type="primary" @click="handleCreateJob">
                        保存
                    </el-button>
                    <el-button type="warning" @click="close">
                        关闭
                    </el-button>
                </div>
            </div>
            <el-form ref="jobInfoForm" :model="jobInfo" label-width="170px">
                <el-form-item label="资源ID" prop="resourceId">
                    <el-input-number :min="101" :max="1099999999" :step="1"
                                     v-model.number="jobInfo.resourceId"/>
                </el-form-item>
                <el-form-item label="资源类型" prop="resourceType">
                    <el-select
                            :value="jobInfo.resourceType"
                            disabled=disabled
                    >
                        <el-option
                                :label="item.label"
                                v-for="(item,index) in RESOURCE_TYPE"
                                :key="index"
                                :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="资源版本" prop="resourceVersion">
                    <el-input v-model.trim="jobInfo.resourceVersion"></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-select
                            :value="jobInfo.status"
                            disabled=disabled
                    >
                        <el-option
                                :label="item.label"
                                v-for="(item,index) in STATUS"
                                :key="index"
                                :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="定时类型" prop="timedType">
                    <el-select
                            :value="jobInfo.timedType"
                            disabled=disabled
                    >
                        <el-option
                                :label="item.label"
                                v-for="(item,index) in TIMED_TYPE"
                                :key="index"
                                :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="cron表达式" prop="cron">
                    <el-input v-model.trim="jobInfo.cron"></el-input>
                </el-form-item>
            </el-form>
        </div>
    </custom-dialog>
</script>

<script>
    Vue.component('timed-task-upsert', {
        template: '#timed-task-upsert',
        data() {
            return {
                ulrs: {
                    getDetailInfo: '/v1/timedTask/getDetailInfo.html',
                    createJob: '/v1/timedTask/createJob.html'
                },
                dialogOptions: {
                    title: this.getTitle(),
                    visible: true
                },
                jobInfo: {
                    id: undefined,
                    resourceId: undefined,
                    resourceType: 'pannel',
                    resourceVersion: undefined,
                    status: 4,
                    timedType: 'recommend_pool_update',
                    cron: '0 0 8 * * ?'
                },
                STATUS: [
                    {
                        label: '待审核',
                        value: 3
                    },
                    {
                        label: '审核通过',
                        value: 4
                    },
                    {
                        label: '审核通过未上线',
                        value: 7
                    },
                    {
                        label: '下线',
                        value: 0
                    }
                ],
                RESOURCE_TYPE: [
                    {
                        label: '板块',
                        value: 'pannel'
                    },
                    {
                        label: '版面',
                        value: 'tab'
                    },
                    {
                        label: '轮播推荐位',
                        value: 'block'
                    },
                    {
                        label: '版面强插任务',
                        value: 'tabForceInsert'
                    }
                ],
                TIMED_TYPE: [
                    {
                        label: '定时上架',
                        value: 'timely_shelving'
                    },
                    {
                        label: '推荐池更新',
                        value: 'recommend_pool_update'
                    },
                    {
                        label: '版面强插',
                        value: 'tab_force_insert'
                    }
                ],
                rules: {
                    resourceId: [
                        {required: true, message: '请输入资源ID'}
                    ],
                    resourceType: [
                        {required: true, message: '请选择资源类型'}
                    ],
                    resourceVersion: [
                        {required: true, message: '请输入资源版本'}
                    ],
                    status: [
                        {required: true, message: '请选择状态'}
                    ],
                    timedType: [
                        {required: true, message: '请选择定时类型'}
                    ]
                }
            }
        },
        props: ['id'],
        computed: {},
        methods: {
            handleCreateJob: function () {
                this.validateFormData(function (err) {
                    if (!err) {
                        const data = this.getFormData()
                        this.ajax(this.urls.createJob, data, function (result) {
                            if (result.success) {
                                this.$message({
                                    showClose: true,
                                    message: '保存成功',
                                    type: 'success'
                                })
                                this.$emit("close");
                            } else {
                                this.$message(result.msg);
                            }
                        }.bind(this));
                    }
                }.bind(this))
            },
            close: function () {
                this.$emit("close");
            },
            getTitle: function () {
                let title = this.id ? '编辑Job' : '创建Job';
                return title;
            },
            validateFormData(cb) {
                this.$refs.jobInfoForm.validate(function (valid) {
                    if (valid) {
                        if (this.jobInfo.timedType === 'recommend_pool_update' && this.jobInfo.cron === '') {
                            this.$message({
                                type: 'error',
                                message: 'cron表达式不能为空！'
                            })
                            cb(true)
                        } else {
                            cb()
                        }
                    } else {
                        this.$message({
                            type: 'error',
                            message: '请把表单填写完整'
                        })
                        cb(true)
                    }
                }.bind(this))
            },
            getFormData() {
                const id = this.jobInfo.id;
                let saveData = {
                            id: id,
                            resourceId: this.jobInfo.resourceId,
                            resourceType: this.jobInfo.resourceType,
                            resourceVersion: this.jobInfo.resourceVersion,
                            status: this.jobInfo.status,
                            timedType: this.jobInfo.timedType,
                            cron: this.jobInfo.cron
                        }
                ;
                const data = {jsonStr: JSON.stringify(saveData)}
                return data
            },
            setFormData: function (editData) {
                this.jobInfo.resourceId = editData.resourceId;
                this.jobInfo.resourceType = editData.resourceType;
                this.jobInfo.resourceVersion = editData.resourceVersion;
                this.jobInfo.status = editData.status;
                this.jobInfo.timedType = editData.timedType;
                this.jobInfo.id = editData.id;
                this.jobInfo.cron = editData.cron;
            }
        },
        created() {
            if (this.id) {
                //初始化数据
                this.ajax('/v1/timedTask/getDetailInfo.html', {
                    id: this.id
                }, function (data) {
                    if (data.code === '0') {
                        let tempData = JSON.parse(JSON.stringify(data.data));
                        this.setFormData(tempData);
                    } else {
                        this.$message(data.msg);
                    }
                });
            }
        }
    })
</script>

<style>
    .action-list .el-form-item {
        margin-bottom: 0px
    }
</style>
